{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>属性管理</legend>
    </fieldset>
    <div class="demoTable">
        {volist name ='sx_type'  id = 'v'}
            {if $v.id==1}
                <button type="button " class="layui-btn layui-btn-normal get-type" data-id="{$v.id}" >{$v.name}</button>
            {else/}
                <button type="button " class="layui-btn layui-btn-primary get-type" data-id="{$v.id}" >{$v.name}</button>
            {/if}
        {/volist}

        <a href="{:url('add')}" class="layui-btn" style="float:right;"><i class="fa fa-plus" aria-hidden="true"></i>{:lang('add')}属性</a>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
{include file="common/foot"/}
<script type="text/html" id="order">
    <input name="{{d.sort}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10"/>
</script>
<script type="text/html" id="is_show">
    {{# if(d.is_show==1){ }}
    <a class="layui-btn layui-btn-mini layui-btn-warm" lay-event="is_show">开启</a>
    {{# }else{  }}
    <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="is_show">禁用</a>
    {{# } }}
</script>
<script type="text/html" id="action">
    <a href="{:url('edit')}?id={{d.id}}" class="layui-btn layui-btn-mini">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
</script>
<script>
    layui.use('table', function() {
        var table = layui.table, $ = layui.jquery;
        var tableIn = table.render({
            id: 'ad',
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page:true,
            cols: [[
                {field: 'id', title: '{:lang("id")}', width: 80, fixed: true},
                {field: 'name', title: '属性名称', width: 400,templet: '#name'},
                {field: 'type', title: '属性类型', width: 100},
                {field: 'addtime', title: '{:lang("add")}{:lang("time")}',width: 150},
                {field: 'sort', align: 'center', title: '{:lang("order")}', width: 120, templet: '#order'},
                {field: 'is_show', align: 'center', title: '{:lang("status")}', width: 100, toolbar: '#is_show'},
                {width: 160, align: 'center', toolbar: '#action'}
            ]],
            limit:10
        });
        //搜索
        $('.get-type').on('click', function () {
            var type = $(this).attr('data-id');
            $(this).removeClass('layui-btn-primary').addClass('layui-btn-normal');
            $(this).siblings().removeClass('layui-btn-normal').addClass('layui-btn-primary');
            tableIn.reload({
                where: {type: type}
            });
        });
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'is_show') {
                var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.post('{:url("editState")}', {'id': data.id}, function (res) {
                    layer.close(loading);
                    if (res.status === 1) {
                        if (res.is_show === 1) {
                            obj.update({
                                is_show: '<a class="layui-btn layui-btn-mini layui-btn-warm" lay-event="open">开启</a>'
                            });
                        } else {
                            obj.update({
                                is_show: '<a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="open">禁用</a>'
                            });
                        }
                    } else {
                        layer.msg('操作失败！', {time: 1000, icon: 2});
                        return false;
                    }
                })
            }else if(obj.event === 'del'){
                layer.confirm('您确定要删除该广告吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
        $('body').on('blur','.list_order',function() {
            var id = $(this).attr('data-id');
            var sort = $(this).val();
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            $.post('{:url("adOrder")}',{id:id,sort:sort},function(res){
                layer.close(loading);
                if(res.code === 1){
                    layer.msg(res.msg, {time: 1000, icon: 1});
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                }
            })
        });
    })
</script>